package com.iou.view;

import com.iou.bean.IOUUser;
import com.vaadin.navigator.View;
import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent;
import com.vaadin.server.ThemeResource;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Embedded;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Panel;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.themes.BaseTheme;

/**
 * Main menu.
 * 
 */
public class MainMenuView extends Panel implements View {

    private IOUUser user;

    /**
     * Constructor.
     * 
     * @param givenUser
     */
    public MainMenuView(IOUUser givenUser) {

        setSizeFull();
        this.user = givenUser;

        GridLayout grid = new GridLayout(3, 3);
        grid.setSizeFull();
        setContent(grid);

        HorizontalLayout rightUp = new HorizontalLayout();
        rightUp.setWidth("100%");
        rightUp.setSpacing(true);
        rightUp.setMargin(true);
        grid.addComponent(rightUp, 2, 0);

        VerticalLayout contentLayout = new VerticalLayout();
        contentLayout.setWidth("100%");
        contentLayout.setSpacing(true);
        grid.addComponent(contentLayout, 1, 1);

        Embedded mainMenu = new Embedded("",
                new ThemeResource("../IOUBoss.png"));

        grid.addComponent(mainMenu, 1, 0);
        grid.setComponentAlignment(mainMenu, Alignment.MIDDLE_CENTER);

        Button navButton = new Button("IOU List");
        navButton.addClickListener(new Button.ClickListener() {
            public void buttonClick(ClickEvent event) {
                UI.getCurrent().getNavigator().navigateTo("ListView");
                ListView.refreshList();
                ListView.addToTable();

            }
        });

        /*
         * Opens a new WindowView for creating a new IOU.
         */
        final Button newIOU = new Button("New IOU");
        newIOU.addClickListener(new Button.ClickListener() {
            public void buttonClick(ClickEvent event) {
                WindowView newWindowView = new WindowView(user);
            }
        });

        Label userName = new Label("Current user: " + this.user.getUsername());
        userName.setSizeUndefined();

        Button logout = new Button("Logout...");
        logout.setStyleName(BaseTheme.BUTTON_LINK);
        logout.addClickListener(new Button.ClickListener() {
            public void buttonClick(ClickEvent event) {
                UI.getCurrent().getNavigator().navigateTo("LoginView");
            }
        });

        rightUp.addComponent(userName);
        rightUp.setComponentAlignment(userName, Alignment.TOP_RIGHT);
        rightUp.addComponent(logout);
        rightUp.setComponentAlignment(logout, Alignment.TOP_RIGHT);

        contentLayout.addComponent(navButton);
        navButton.setWidth("40%");
        contentLayout.setComponentAlignment(navButton, Alignment.MIDDLE_CENTER);
        contentLayout.addComponent(newIOU);
        newIOU.setWidth("40%");
        contentLayout.setComponentAlignment(newIOU, Alignment.MIDDLE_CENTER);

        Label leftFillLabel = new Label("");
        leftFillLabel.setWidth("100%");
        grid.addComponent(leftFillLabel, 0, 1, 0, 2);

        Label rightFillLabel = new Label("");
        rightFillLabel.setWidth("100%");
        grid.addComponent(rightFillLabel, 2, 1, 2, 2);

        grid.setColumnExpandRatio(0, 1);
        grid.setColumnExpandRatio(1, 1);
        grid.setColumnExpandRatio(2, 1);

        grid.setRowExpandRatio(0, 1);
        grid.setRowExpandRatio(1, 1);
        grid.setRowExpandRatio(2, 1);

    }

    @Override
    public void enter(ViewChangeEvent event) {

    }
}
